package com.ld.control.business.record.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ld.control.business.process.service.ProcessNodeExampleRunningParamService;
import com.ld.control.business.process.vo.req.UpdateNodeParamReqVo;
import com.ld.control.business.process.vo.resp.ProcessNodeExampleRunningParamRespVo;
import com.ld.control.business.record.service.ErrorNoticeRecordService;
import com.ld.control.business.record.vo.resp.ErrorNoticeRecordRespVo;
import com.ld.control.common.ResultResponse;
import com.ld.control.common.vo.GenerateListVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.sysenum.ResultResponseEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 任务报错后的记录和处理 前端控制器
 * </p>
 *
 * @author wangfeng
 * @since 2024-05-13
 */
@Slf4j
@Tag(name = "错误通知相关接口")
@RestController
public class ErrorNoticeRecordController {

    @Autowired
    private ErrorNoticeRecordService errorRecordService;

    @Autowired
    private ProcessNodeExampleRunningParamService processNodeExampleRunningParamService;

    @GetMapping("/api/centralControl/errorRecord/getPageList")
    @Operation(summary = "错误通知列表")
    public ResultResponse<GeneratePageListVo<ErrorNoticeRecordRespVo>> getPageList(
            @RequestParam(required = false) String taskExampleCode,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false, defaultValue = "1") Integer current,
            @RequestParam(required = false, defaultValue = "10") Integer size
    ){
        IPage<ErrorNoticeRecordRespVo> pageList = errorRecordService.getPageList(taskExampleCode, status, current, size);
        return ResultResponse.success(new GeneratePageListVo<>(pageList));
    }

    @GetMapping("/api/centralControl/errorRecord/batchUpdateRunningParam")
    @Operation(summary = "批量修改命令的参数")
    public ResultResponse<ResultResponseEnum> batchUpdate(
            @RequestBody UpdateNodeParamReqVo reqVo
    ){
        processNodeExampleRunningParamService.batchUpdate(reqVo);
        return ResultResponse.success();
    }

    @PostMapping("/api/centralControl/errorRecord/handleType")
    @Operation(summary = "错误处理")
    public ResultResponse<ResultResponseEnum> retry(
            @RequestParam Long id,
            @Parameter(description = "0重试，1跳过")@RequestParam Integer handleType
    ){
        errorRecordService.errorHandle(id, handleType);
        return ResultResponse.success();
    }

    @PostMapping("/api/centralControl/errorRecord/debugRetry")
    @Operation(summary = "某条命令重发(debug用，慎用)")
    public ResultResponse<ResultResponseEnum> debugRetry(
            @RequestParam Long processNodeExampleRunningId
    ){
        errorRecordService.debugRetry(processNodeExampleRunningId);
        return ResultResponse.success();
    }
}

